package com.iamteer.b_upload;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


/**
 * 文件上传组件使用
 * 
 * FileItem
 * 		item.getFieldName()		// 获取上传表单元素名称
 * 		item.getString			// 获取上传数据
 *  	item.getString("UTF-8")	// 获取上传数据，处理编码
 *  	item.getContentType()	// 获取文件类型[文件项]
 *  	item.getInputStream		// 获取文件流[文件项]
 *  	item.getName()			// 获取文件名[文件项]
 *  	item.write(arg0)		// 写文件
 *  	item.delete()			// 删除临时文件
 * 
 * @author Chuck
 *
 */
@WebServlet("/FileUploadHello")
public class FileUpload_hello extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1. 创建文件上传工厂类
		DiskFileItemFactory factory = new DiskFileItemFactory();
		// 2. 创建文件上传核心类对象
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setFileSizeMax(4096);		// 单个文件最大上传限制
		upload.setSizeMax(10240);			// 所有文件最大上传限制
		upload.setHeaderEncoding("utf-8");	// 设置上传文件名编码
		// 3. 判断当前表单是否为文件上传表单
		if (ServletFileUpload.isMultipartContent(request)) {
			try {
				List<FileItem> list = upload.parseRequest(request);	// 获取所有 FileItem 对象
				// 遍历每个上传项
				for (FileItem item : list) {
					// 判断是普通表单还是文件上传表单
					if (item.isFormField()) {
						// 普通表单
						String fieldName = item.getFieldName();	// 文本框名称
						String value = item.getString("utf-8");		// 文本框值
						System.out.println(fieldName + " : " + value);
					} else {
						// 文件上传表单
						String fieldName = item.getFieldName();		// 文本框名称
						System.out.println("文本框名称：" + fieldName);
						String contentType = item.getContentType();	// 文件类型
						System.out.println("文件类型：" + contentType);
						String name = item.getName();				// 文件名
						System.out.println("文件名：" + name);
						InputStream in = item.getInputStream();		// 文件流
						InputStreamReader inReader = new InputStreamReader(in, "gbk");
						BufferedReader reader = new BufferedReader(inReader);
						String line = null;
						while ((line = reader.readLine()) != null) {
							System.out.println(line);
						}
					}
				}
			} catch (FileUploadException e) {
				e.printStackTrace();
			}
		} else {

		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
